
const fs = require('fs');


const router = require('koa-router')()

router.get('/', async (ctx, next) => {
  await ctx.render('index', {
    title: 'Hello Koa 2!'
  })
})

router.get('/string', async (ctx, next) => {
  ctx.body = 'koa2 string'
})

router.get('/json', async (ctx, next) => {
  ctx.body = {
    title: 'koa2 json'
  }
})


router.get('/list', async (ctx, next) => {

  // 设置返回的response body 
  // ctx.body =["list data 1","list data 2","list data 3","list data 4"]

  // 设置返回 xml 格式的文档
  //   ctx.set('Content-Type', 'application/xml');
  //   ctx.body = `<note>
  //   <to>Tove</to>
  //   <from>Jani</from>
  //   <heading>Reminder</heading>
  //   <body>Don't forget me this weekend!</body>
  // </note>`



  // 返回 json 数据
  // ctx.body =[
  //   {"name":"frank1","age":19},
  //   {"name":"frank2","age":12},
  //   {"name":"frank3","age":13},
  //   {"name":"frank4","age":14}

  // ]

  // 模拟等待操作 3000 毫秒 
  await new Promise((resolve, reject) => {
    setTimeout(() => {
      resolve();
    }, 3000)

  })

  // 获取请求参数
  params = ctx.request.query;
  console.log('params:', params);


  // 设置JSON 数据 
  ctx.body = {
    errorcode: 0,
    errmsg: 'ok',
    list: [
      { "name": "liuxiaolu", "age": 19, "gender": "male" },
      { "name": "lile", "age": 20, "gender": "male" },
      { "name": "weiliang", "age": 21, "gender": "male" },
      // { "name": "frank", "age": 22, "gender": "male" },
      // { "name": "guanchen", "age": 23, "gender": "male" },
      // { "name": "yanglikai", "age": 24, "gender": "male" },
      // { "name": "xiaoqi", "age": 66, "gender": "male" },
    ]

  }




  //  ctx.body ="list data"
})


router.post('/register', async (ctx, next) => {


  console.log('register begining...');

  // console.log(ctx.request.body);
  // data = ctx.request.body

  // data.age = parseInt(data.age) + 1;
  // // ctx.body ="username:frank"

  // // 设置body, 以及状态码 
  // ctx.body = data;
  // ctx.response.status = 200;



  // 获取 post的body 
  let body = ctx.request.body;
  console.log('body:', body);

  // ctx.body = 'return_data';
  ctx.body = body;


  console.log('register done...');
})




router.post('/login', async (ctx, next) => {
  console.log('login begining...');
  ctx.body = 'login done';
  console.log('login done...');

})







// 获取数据  
router.post('/list', async (ctx, next) => {
  // 获取 post的body 
  let body = ctx.request.body;
  // console.log('body:', body);

  var args = [
    { fied: 'page', type: 'number' },
    { fied: 'count', type: 'number' },

  ]

  for (var i = 0; i < args.length; i++) {
    var item = args[i];
    // 参数格式 检查 
    if (!Object.keys(body).includes(item.fied)) {
      ctx.body = {
        errcode: -1,
        errmsg: '参数个数错误'
      };
      return;
    }

    // 参数类型错误
    if (typeof body[item.fied] != item.type) {
      ctx.body = {
        errcode: -2,
        errmsg: '参数类型错误'
      };
      return;
    }
  }


  // process 
  // ctx.body = 'ok';
  var data = fs.readFileSync('./data/list.json');
  data = JSON.parse(data);

  var list = data.splice(body.page * body.count, body.count);


  ctx.body = {

    errcode: 0,
    errmsg: 'ok',
    list: list

  };

})



module.exports = router
